//
//  YYNetWorkManger.swift
//  STMusic
//
//  Created by stoneobs on 17/4/5.
//  Copyright © 2017年 stoneobs. All rights reserved.
//  碎月专用

import UIKit
struct Platform {
    static let isSimulator: Bool = {
        var isSim = false
        #if arch(i386) || arch(x86_64)
            isSim = true
        #endif
        return isSim
    }()
}
class YYSuiYueNetWorkManger: NSObject {
    private var  mgr:AFHTTPSessionManager!
    
    static let defultManger = YYSuiYueNetWorkManger.init();
    public override init() {
        super.init();
        self.mgr = AFHTTPSessionManager.init(baseURL: nil);
        mgr.requestSerializer = AFJSONRequestSerializer.init(writingOptions: .init(rawValue: 0));
        //固定头部
        mgr.requestSerializer.setValue("application/json", forHTTPHeaderField: "Content-Type")
        mgr.requestSerializer.setValue("application/json", forHTTPHeaderField: "Accept")
        //之前1.7.2，2.2.0才可以使用baner
        mgr.requestSerializer.setValue("Fiil/2.2.0/(iPhone;10.2;Sacle/3.00)", forHTTPHeaderField: "User-Agent")
        mgr.requestSerializer.setValue("zh-Hans-CN;q=1", forHTTPHeaderField: "Accept-Language")
        mgr.requestSerializer.setValue("gzip, deflate", forHTTPHeaderField: "Accept-Encoding")
        mgr.requestSerializer.setValue("XMLHttpRequest", forHTTPHeaderField: "X-Requested-With")
        var uuid = "03D11B58-9B26-4E34-9368-292FFF0E0EB6";
        if Platform.isSimulator  {
        }else{
          uuid = "03D11B58-9B26-4E34-9368-292FFF0E0EB9";
        }
        
        mgr.requestSerializer.setValue(uuid, forHTTPHeaderField: "X-UUID")//正式版获取uuid
        mgr.requestSerializer.setValue("0", forHTTPHeaderField: "X-Channel");
        mgr.requestSerializer.setValue("com.suiyueyuele.suiyue.ios", forHTTPHeaderField: "X-BundleId");
        
        
        
        
    }
    //登陆后添加token
    public func addTokenToHeader(token:String) -> Void {
        mgr.requestSerializer.setValue(token, forHTTPHeaderField: "X-Authorization")//登陆之后的token
    }
    //post
    public func postNetwork(urlString:String,dic:Dictionary<String, Any>,successHandle:@escaping ( _ json:NSDictionary) -> Void,failHandle:@escaping (_ des:String) -> Void) ->Void{
    
        YYSuiYueNetWorkManger.defultManger.mgr.post(urlString, parameters: dic, progress: { (progess) in
            
        }, success: { (task, json) in
    
                successHandle(json as! NSDictionary);
   
            
        }) { (task, error) in
            
            failHandle(error.localizedDescription);
        }


    
       
    }
    //get
    public func getNetwork(urlString:String,dic:Dictionary<String, Any>,successHandle:@escaping ( _ json:NSDictionary) -> Void,failHandle:@escaping (_ des:String) -> Void) ->Void{
        
        YYSuiYueNetWorkManger.defultManger.mgr.get(urlString, parameters: dic, progress: { (progess) in
            
        }, success: { (task, json) in
            
            successHandle(json as! NSDictionary );
            
            
        }) { (task, error) in
            
            failHandle(error.localizedDescription);
        }
        
        
        
        
    }
    
    
}
